package me.pplay.playtalk.view;

import me.pplay.playtalk.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
   
 
public class CustomDialog extends Dialog {  
   
    public CustomDialog(Context context, int theme) {  
        super(context, theme);  
    }  
   
    public CustomDialog(Context context) {  
        super(context);  
    }  
   
    /** 
     * Helper class for creating a custom dialog 
     */  
    public static class Builder {  
   
        private Context context;  
        private int icon;
        private String title;  
        private String message;  
        private View contentView;  
   
        private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;  
   
        public Builder(Context context) {  
            this.context = context;  
        }  
   
        /** 
         * Set the Dialog message from String 
         * @param title 
         * @return 
         */  
        public Builder setMessage(String message) {  
            this.message = message;  
            return this;  
        }  
   
        /** 
         * Set the Dialog message from resource 
         * @param title 
         * @return 
         */  
        public Builder setMessage(int message) {  
            this.message = (String) context.getText(message);  
            return this;  
        }  
   
        /** 
         * Set the Dialog title from resource 
         * @param title 
         * @return 
         */  
        public Builder setTitle(int title) {  
            this.title = (String) context.getText(title);  
            return this;  
        }  
   
        /** 
         * Set the Dialog title from String 
         * @param title 
         * @return 
         */  
        public Builder setTitle(String title) {  
            this.title = title;  
            return this;  
        }  
        
        public Builder setIcon(int icon) {
        	this.icon = icon;
        	return this;
        }
   
        /** 
         * Set a custom content view for the Dialog. 
         * If a message is set, the contentView is not 
         * added to the Dialog... 
         * @param v 
         * @return 
         */  
        public Builder setContentView(View v) {  
            this.contentView = v;  
            return this;  
        }  
   
        /** 
         * Create the custom dialog 
         */  
        public CustomDialog create() {  
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            // instantiate the dialog with the custom Theme  
            final CustomDialog dialog = new CustomDialog(context, R.style.AliDialog);  
            View layout = inflater.inflate(R.layout.alert_dialog, (ViewGroup)(((Activity) context).findViewById(R.id.parentPanel)));  
            dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
            // set the dialog title  
            ((TextView) layout.findViewById(R.id.alertTitle)).setText(title);  
            ((ImageView) layout.findViewById(R.id.icon)).setBackgroundResource(icon);
            if (message != null) {  
                ((TextView) layout.findViewById(R.id.message)).setText(message); 
            } else if (contentView != null) {  
                // add the contentView to the dialog body  
                ((LinearLayout) layout.findViewById(R.id.contentPanel)).removeAllViews();  
                ((LinearLayout) layout.findViewById(R.id.contentPanel)).addView(contentView,   
                                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
            } 
            dialog.setContentView(layout);  
            return dialog;  
        }  
   
    }  
   
}  
